package org.mozhu.zufang.showcase.crud.entity;

import lombok.Data;
import org.mozhu.mboot.core.data.entity.Activatable;
import org.mozhu.mboot.core.data.entity.BaseEntity;
import org.mozhu.mboot.core.data.entity.LogicDeleteable;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.*;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 示例 - 实体类
 *
 * @author ZhangPei
 */
@Entity
@Table(name = "showcase_sample")
@Data
public class Sample extends BaseEntity implements LogicDeleteable, Activatable {

    private static final long serialVersionUID = -3979963440988089522L;

    public static final String EMAIL_PATTERN = "^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?";

    // -- 必填项 -- //
    // 姓名
    protected String name;

    // 用户邮箱
    @NotEmpty(message = "{not.null}")
    @Pattern(regexp = EMAIL_PATTERN, message = "{email.not.valid}")
    protected String email;

    // 性别
    protected String gender;

    // 生日
    protected Date birthday;

    @NotNull(message = "{not.null}")
    @Min(18L)
    @Max(100L)
    protected Integer age;

    @Min(1L)
    protected BigDecimal salary;
    /**
     * 逻辑删除flag
     */
    private Boolean deleted = Boolean.FALSE;

    @Override
    public Boolean getDeleted() {
        return deleted;
    }

    @Override
    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    @Override
    public void markDeleted() {
        this.deleted = Boolean.TRUE;
    }

    /**
     * 激活(TRUE)，冻结(FALSE)
     */
    private Boolean activated = Boolean.TRUE;

    @Override
    public Boolean getActivated() {
        return activated;
    }

    @Override
    public void setActivated(Boolean activated) {
        this.activated = activated;
    }

    @Override
    public void markActivated() {
        this.activated = Boolean.TRUE;
    }

    @Override
    public void markDeactivated() {
        this.activated = Boolean.FALSE;
    }

    public boolean isAvailable() {
        return !Boolean.TRUE.equals(this.deleted) && !isBlocked();
    }

    public boolean isBlocked() {
        return !Boolean.TRUE.equals(getActivated());
    }

    @Override
    public void recycle() {
        setDeleted(Boolean.FALSE);
    }
}
